<!--字典项页-->
<template>
  <div style="background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">字典项管理</span>
    <el-button @click="goBack" type="primary" style="float:right;margin-top:13px;">返回</el-button>
  </div>
  <!-- 字典项搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="字典项名称">
        <el-input placeholder="请输入" style="width:220px;" v-model="searchForm.label"
                  @keydown.enter.prevent="selectDictOption"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="selectDictOption">搜索</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:20px;">
    <div style="margin:10px 0px 40px 10px;">
      字典项列表
      <el-button type="primary" style="float: right;margin-top:-3px;" @click="dialogVisible=true">新增字典项</el-button>
    </div>
    <el-table :data="dictOptionArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="label" label="字典项名称" align="center"/>
      <el-table-column prop="value" label="字典项值" align="center"/>
      <el-table-column prop="sort" label="字典项排序" align="center"/>
      <el-table-column prop="remark" label="备注信息" align="center" width="320"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="editDictOption(scope.row.id)">编辑</el-button>
          <el-button type="primary" link @click="deleteDictOption(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 保存字典项弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-form-item label="字典项名称">
        <el-input placeholder="请输入" v-model="saveDictOptionForm.label"></el-input>
      </el-form-item>
      <el-form-item label="字典项值">
        <el-input placeholder="请输入" v-model="saveDictOptionForm.value"></el-input>
      </el-form-item>
      <el-form-item label="字典项排序">
        <el-input placeholder="请输入" v-model="saveDictOptionForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input placeholder="请输入" v-model="saveDictOptionForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveDictOption">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import {onMounted, ref} from "vue";
import router from "@/router";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//定义返回到字典列表页面的方法
const goBack = ()=>{
  router.push('/dict');
}
//http://localhost:9090/dictOption?id=104
//获取在URL查询字符串的参数值,如上方URL中的字典id 104
const dictId = new URLSearchParams(window.location.search).get('id');
//定义对象用来保存字典项的搜索条件
//注意，这里要添加一个字典id属性作为字典项查询的隐藏条件！！！
const searchForm = ref({label:'',dictId:dictId});
//定义数组对象用来保存字典项表格数据
const dictOptionArr = ref([]);
//定义加载字典项的方法
const selectDictOption = ()=>{
  let data = qs.stringify(searchForm.value);
  axios.get(BASE_URL+'/oa1/dictoption/select?'+data).then((response)=>{
    if(response.data.code == 2000){
      dictOptionArr.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
onMounted(()=>{
  selectDictOption();
})
//定义重置搜索的方法
const resetSearch = ()=>{
  //注意:这里不能清空字典项id!否则不清楚要清空哪个字典下的字典项,所有的字典项都会被查出来!
  //只需要清空用户能看得到的搜索条件即可
  searchForm.value = {label:'',dictId:dictId};
  //再根据字典id查询该字典下的所有字典项
  selectDictOption();
}

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);
//定义变量用来保存弹窗的标题
const dialogTitle = ref('新增字典项');

//定义表单对象用来保存新增字典项时的数据
const saveDictOptionForm = ref({
  label:'',
  value:'',
  sort:'',
  remark:'',
  dictId:dictId //新增需携带字典id作为新增字典项的隐藏条件
});
//定义保存字典项的方法
const saveDictOption = ()=>{
  let data = qs.stringify(saveDictOptionForm.value);
  axios.post(BASE_URL+'/oa1/dictoption/save',data).then((response)=>{
    if(response.data.code == 2000){
      ElMessage.success('保存成功!');
      //保存成功后,需关闭新增弹窗,清空表单数据,重新加载所有的字典项数据
      dialogVisible.value = false;
      saveDictOptionForm.value = {label:'', value:'', sort:'', remark:'', dictId:dictId};
      selectDictOption();
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//定义处理弹窗关闭的方法
const handleClose = ()=>{
  if(confirm('是否关闭本弹窗?')){
    dialogVisible.value = false;
    //注意:关闭弹窗时不应该清空当前字典的id,否则无法加载当前字典下对应的字典项
    saveDictOptionForm.value = {label:'', value:'', sort:'', remark:'', dictId:dictId};
  }
}
//定义编辑字典项的方法
const editDictOption = (id)=>{
  dialogTitle.value = '编辑字典项';
  dialogVisible.value = true;
  axios.get(BASE_URL+'/oa1/dictoption/select?id='+id).then((response)=>{
    if(response.data.code == 2000){
      //注意！后端返回的是List,我们需要取出list中的第1条数据！！
      saveDictOptionForm.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg)
    }
  })
}
//定义删除字典项的方法
const deleteDictOption = (id)=>{
  if(confirm('是否删除本字典项?')){
    axios.post(BASE_URL+'/oa1/dictoption/delete/'+id).then((response)=>{
      if(response.data.code == 2000){
        ElMessage.success('删除成功!');
        selectDictOption();
      }else{
        ElMessage.error(response.data.msg);
      }
    })
  }
}
</script>

<style scoped>

</style>